<!--
 * @Description: 素材库审核列表
 * @Author: Li Yujie
 * @Date: 2020/12/15
 * @Last Modified by: Li Yujie
 * @Last Modified time: 2020/12/15
-->
<template>
    <div
        class="wrapper"
        ref="wrapper"
    >
        <div class="content-wrapper">
            <!-- header -->
            <div class="header">
                <div class="header-title">
                    素材库审核列表
                </div>
            </div>
            <!-- /header -->
            <el-tabs
                v-model="filterData.verify"
                @tab-click="handleTabClick"
            >
                <el-tab-pane
                    label="待审核"
                    name="1"
                />
                <el-tab-pane
                    label="审核已通过"
                    name="2"
                />
                <el-tab-pane
                    label="审核不通过"
                    name="3"
                />
            </el-tabs>
            <!-- 筛选-->
            <div class="filter-view">
                <div class="header-new-box">
                    <p class="header-new-box-title">商品信息：</p>
                    <el-input
                        placeholder="请输入商品名称或商品编号"
                        v-model="filterData.goods_keyword"
                        suffix-icon="el-icon-search"
                        clearable
                        size="small"
                        @keydown.enter.native="getTableData(1)"
                    />
                </div>
                <div class="header-new-box">
                    <p class="header-new-box-title">店铺名称：</p>
                    <el-input
                        placeholder="请输入店铺名称"
                        v-model="filterData.shop_title"
                        suffix-icon="el-icon-search"
                        clearable
                        size="small"
                        @keydown.enter.native="getTableData(1)"
                    />
                </div>
                <div class="header-new-box">
                    <p class="header-new-box-title">创建人：</p>
                    <el-input
                        placeholder="请输入创建人"
                        v-model="filterData.user_keyword"
                        suffix-icon="el-icon-search"
                        clearable
                        size="small"
                        @keydown.enter.native="getTableData(1)"
                    />
                </div>
                <div class="header-new-box">
                    <p class="header-new-box-title">创建时间：</p>
                    <el-date-picker
                        v-model="dateTimeRange"
                        type="datetimerange"
                        range-separator="-"
                        start-placeholder="开始日期"
                        end-placeholder="结束日期"
                        :default-time="['00:00:00', '23:59:59']"
                        size="small"
                        style="width: 400px"
                        value-format="timestamp"
                    />
                </div>
                <div class="header-new-box">
                    <el-button
                        type="primary"
                        size="small"
                        @click="getTableData(1)"
                    >
                        查询
                    </el-button>
                </div>
            </div>
            <!--筛选-->
            <!-- table -->
            <el-table
                :data="tableData"
                height="100%"
                style="width: 100%;"
                ref="tempLateName"
            >
                <el-table-column
                    label="序号"
                    type="index"
                    align="center"
                    :formatter="emptyFormatter"
                    width="80"
                />
                <el-table-column
                    prop="title"
                    label="商品信息"
                    align="left"
                    :formatter="emptyFormatter"
                >
                    <template slot-scope="scope">
                        <div class="goods-info">
                            <img
                                :src="scope.row.goods_info.goods_cover"
                                class="goods-img"
                                @click="fnShowPreview(scope.row.goods_info.goods_cover)"
                            >
                            <div>
                                <el-tooltip
                                    effect="dark"
                                    :content="scope.row.goods_info.goods_title"
                                    placement="top">
                                    <div class="goods-name">
                                        {{ scope.row.goods_info.goods_title }}
                                    </div>
                                </el-tooltip>
                                <div class="goods-price">
                                    {{ `￥${scope.row.goods_info.goods_price}` }}
                                </div>
                            </div>
                        </div>
                    </template>
                </el-table-column>
                <el-table-column
                    prop="goods_no"
                    label="商品编号"
                    show-overflow-tooltip
                    :formatter="emptyFormatter"
                />
                <el-table-column
                    prop="goods_info.shop_title"
                    label="店铺名称"
                    show-overflow-tooltip
                    align="left"
                    :formatter="emptyFormatter"
                />
                <el-table-column
                    label="创建人"
                    show-overflow-tooltip
                    align="center"
                    :formatter="emptyFormatter"
                >
                    <template slot-scope="scope">
                        <div v-if="scope.row.user_type === 1">
                            {{ scope.row.user_info.true_name }}
                        </div>
                        <div v-if="scope.row.user_type === 3">
                            管理员（{{ scope.row.user_info.true_name }}）
                        </div>
                    </template>
                </el-table-column>
                <el-table-column
                    label="创建时间"
                    show-overflow-tooltip
                    align="center"
                >
                    <template slot-scope="scope">
                        {{ scope.row.create_time * 1000 | dateFormatter('yyyy/MM/dd HH:mm:ss') }}
                    </template>
                </el-table-column>
                <el-table-column
                    label="操作"
                    align="left"
                >
                    <template slot-scope="scope">
                        <el-button
                            type="text"
                            @click="showDetailDialog(scope.row)"
                        >
                            查看
                        </el-button>
                        <el-button
                            type="text"
                            @click="showPassDialog(scope.row.material_no)"
                            v-if="scope.row.verify === 1"
                        >
                            通过
                        </el-button>
                        <el-button
                            type="text"
                            @click="showRejectDialog(scope.row.material_no)"
                            v-if="scope.row.verify === 1"
                        >
                            驳回
                        </el-button>
                        <el-button
                            type="text"
                            @click="showReasonDialog(scope.row.verify_reason)"
                            v-if="scope.row.verify === 3 && scope.row.verify_reason"
                        >
                            驳回原因
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>
            <!-- /table -->

            <!-- 分页 -->
            <div class="pagination-wrapper clearfix">
                <el-pagination
                    layout="total, prev, pager, next, jumper"
                    :current-page.sync="page.now_page"
                    :total="page.total_count"
                    :page-size="page.page_size"
                    @current-change="getTableData"
                />
            </div>
            <!-- /分页 -->
        </div>
        <!-- 驳回弹窗 -->
        <reject-dialog
            :visible.sync="rejectDialog"
            @confirm="handleExamine('reject', $event)"
        />
        <!-- /驳回弹窗 -->

        <!--驳回原因弹窗-->
        <el-dialog
            title="驳回原因"
            :visible.sync="reasonDialog"
            width="640px"
            :close-on-click-modal="false"
        >
            <div
                class="reject-reason"
            >
                <div class="reason-wrapper">{{ reasonDetail }}</div>
                <el-button
                    type="primary"
                    class="know-btn"
                    size="middle"
                    @click="reasonDialog = false"
                >
                    知道了
                </el-button>
            </div>
        </el-dialog>
        <!--/驳回原因弹窗-->

        <!--查看详情弹窗-->
        <el-dialog
            title="查看素材"
            :visible.sync="detailDialog"
            width="800px"
            :close-on-click-modal="false"
        >
            <div
                class="detail-dialog"
                v-if="detailInfo"
            >

                <el-table
                    :data="goodInfo"
                    class="goods-table"
                    ref="tempLateName"
                    v-show="goodInfo.length"
                >
                    <el-table-column
                        label="商品信息"
                        align="left"
                        width="200"
                    >
                        <template slot-scope="scope">
                            <div class="goods-info">
                                <img
                                    :src="scope.row.goods_cover"
                                    class="goods-img"
                                    @click="fnShowPreview(scope.row.goods_cover)"
                                >
                                <div>
                                    <el-tooltip
                                        effect="dark"
                                        :content="scope.row.goods_title"
                                        placement="top">
                                        <div class="goods-name">
                                            {{ scope.row.goods_title }}
                                        </div>
                                    </el-tooltip>
                                    <div class="goods-price">
                                        {{ `￥${scope.row.goods_price}` }}
                                    </div>
                                </div>
                            </div>
                        </template>
                    </el-table-column>
                    <el-table-column
                        label="店铺名称"
                        prop="shop_title"
                        align="center"
                    />
                    <el-table-column
                        label="商品编号"
                        show-overflow-tooltip
                        prop="goods_no"
                        align="center"
                    />
                </el-table>
                <div class="content">
                    <div class="content-item">
                        <div class="item-title">创建人：</div>
                        <div class="item-view">
                            <div v-if="detailInfo.user_type === 1">
                                {{ detailInfo.user_info.true_name }}
                            </div>
                            <div v-if="detailInfo.user_type === 3">掌大官方</div>
                        </div>
                    </div>
                    <div class="content-item">
                        <div class="item-title">创建时间：</div>
                        <div class="item-view">
                            {{ detailInfo.create_time * 1000 | dateFormatter('yyyy/MM/dd HH:mm:ss') }}
                        </div>
                    </div>
                    <div
                        class="content-item"
                        v-if="detailInfo.type === 1"
                    >
                        <div class="item-title">宣传图片：</div>
                        <div class="item-view">
                            <!-- 图片列表 -->
                            <div class="pic-list">
                                <div
                                    class="pic-item"
                                    v-for="(item, index) in detailInfo.share_data.image_urls"
                                    :key="index"
                                    @click="fnShowPreview(detailInfo.share_data.image_urls, index)"
                                >
                                    <img :src="item">
                                </div>
                            </div>
                        </div>
                    </div>
                    <div
                        class="content-item"
                        v-if="detailInfo.type === 2"
                    >
                        <div class="item-title">宣传视频：</div>
                        <div class="item-view">
                            <!--视频-->
                            <div
                                class="video-wrap"
                                @click="previewVideo"
                            >
                                <div class="video">
                                    <img :src="videoCover">
                                </div>
                                <div class="icon">
                                    <img src="@/assets/image/play.png">
                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="content-item">
                        <div class="item-title">宣传文案：</div>
                        <div class="item-view">
                            {{ detailInfo.share_content }}
                        </div>
                    </div>
                </div>
                <div
                    class="btn-group"
                    v-if="detailInfo.verify === 1"
                >
                    <el-button
                        size="medium"
                        @click="showRejectDialog(material_no)"
                    >
                        驳回
                    </el-button>
                    <el-button
                        type="primary"
                        size="medium"
                        @click="handleExamine('pass')"
                    >
                        通过
                    </el-button>
                </div>
            </div>
        </el-dialog>
        <!--/查看详情弹窗-->

        <!-- 图片预览 -->
        <img-preview ref="preview"/>
        <!-- 视频播放组件 -->
        <video-simple
            ref="videoSimple"
            :url="video"
            :top="54"
            :left="left"
        />
    </div>
</template>

<script>
import imgPreview from '@/components/common/img-preview';
import videoSimple from '@/components/dorm/video-simple.vue';
import rejectDialog from '@/components/dialog/reject-dialog';

export default {
    name: 'MaterialExamineList',
    data() {
        return {
            filterData: {
                // 状态
                verify: '1',
                goods_keyword: '',
                shop_title: '',
                user_keyword: '',
                create_start_time: '',
                create_end_time: '',
            },
            tableData: [],
            page: {
                now_page: 1,
                total_count: 0,
                page_size: 20
            },
            dateTimeRange: [],
            // 拒绝弹窗
            rejectDialog: false,
            material_no: '',
            // 驳回原因详情
            reasonDetail: '',
            // 驳回原因弹窗
            reasonDialog: false,
            // 查看详情弹窗
            detailDialog: false,
            // 详情商品信息表格
            goodInfo: [{}],
            // 详情数据
            detailInfo: {},
            videoCover: '',
            video: null,
            left: 0,
            authResult: {}
        };
    },
    components: {
        imgPreview,
        videoSimple,
        rejectDialog
    },
    mounted() {
        this.$store.commit('SET_HEADER', [{title: "APP后台"}, {title: '商城'}, {title: '素材库审核列表'}]);
        this.authResult = this.$route.meta.authResult;
        this.getTableData();
    },
    watch: {
        dateTimeRange(value) {
            this.filterData.create_start_time = value ? value[0] / 1000 : '';
            this.filterData.create_end_time = value ? value[1] / 1000 : '';
            this.getTableData(1);
        }
    },
    methods: {

        /**
         * @description 获取素材库列表数据
         * @param page
         */
        getTableData(page) {

            let params = {...this.filterData};

            params.page_id = page || this.page.now_page;
            params.verify = Number(params.verify);

            this.$post('/student/shop_goods_share_material%5Cget_verify_list', params, resp => {
                if (resp.code === 1) {
                    this.tableData = resp.data.list;
                    this.page = resp.data.page;
                } else {
                    this.$notify({
                        title: '提示',
                        message: resp.msg,
                        duration: 2000,
                        type: 'warning'
                    });
                }
            });
        },
        /**
         * 表格数据过滤函数
         * @param row
         * @param column
         * @param val
         * @returns {string|*}
         */
        emptyFormatter(row, column, val) {
            if (!val) return '--';
            return val;
        },

        /**
         * tab审核状态切换
         * @param tab
         * @param event
         */
        handleTabClick(tab, event) {
            this.getTableData(1);
        },

        /**
         * 预览图片
         * @param   {String}    images      //单图预览
         * @param   {Array}     images      //多图预览
         * @param index
         */
        fnShowPreview(images, index) {
            if (images && Object.prototype.toString.call(images) == '[object String]') {
                this.$refs.preview.show([
                    images
                ]);
            } else if (images && Object.prototype.toString.call(images) == '[object Array]') {
                this.$refs.preview.show(images, index);
            } else {
                this.$message({
                    type: 'warning',
                    message: '图片类型错误'
                });
            }
        },
        /**
         * 审核通过提示弹窗
         * @param no
         */
        showPassDialog(no) {
            if(!this.authResult.auth_verify) {
                this.$notify({
                    title: '提示',
                    message: '请联系管理员添加权限',
                    type: 'warning',
                    duration: 2000
                });
                return false;
            }
            this.$confirm('是否确认通过该条素材审核并发布?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                this.material_no = no;
                this.handleExamine('pass');
            });
        },
        /**
         * 显示拒绝弹窗
         * @param no
         */
        showRejectDialog(no) {
            if(!this.authResult.auth_verify) {
                this.$notify({
                    title: '提示',
                    message: '请联系管理员添加权限',
                    type: 'warning',
                    duration: 2000
                });
                return false;
            }

            this.material_no = no;
            this.rejectDialog = true;
        },
        /**
         * 审核素材请求
         * @param action - pass通过  reject拒绝
         * @param reason - 拒绝原因
         *        verify 2通过 3不通过
         */
        handleExamine(action, reason = '') {
            let params = {
                material_no: this.material_no,
                verify_fail_reason: reason,
                verify: action === 'pass' ? 2 : 3
            };

            this.$post('/student/shop_goods_share_material%5Cverify', params, resp => {
                if (resp.code === 1) {

                    this.rejectDialog = false;
                    this.detailDialog = false;
                    this.$notify({
                        title: '成功',
                        message: '操作成功',
                        duration: 2000,
                        type: 'success'
                    });
                    this.getTableData();
                } else {
                    this.$notify({
                        title: '提示',
                        message: resp.msg,
                        duration: 2000,
                        type: 'warning'
                    });
                }
            });
        },
        /**
         * 显示驳回原因弹窗
         * @param reason
         */
        showReasonDialog(reason) {

            this.reasonDetail = reason;
            this.reasonDialog = true;
        },

        /**
         * 显示查看详情弹窗
         * @param row
         */
        showDetailDialog(row) {


            let params = {
                material_no: row.material_no
            };

            this.$post('/student/shop_goods_share_material%5Cget', params, resp => {
                if (resp.code === 1) {
                    let goods_info = resp.data.goods_info;

                    goods_info.goods_no = resp.data.goods_no;
                    this.goodInfo = [goods_info];
                    this.detailInfo = resp.data;
                    this.detailInfo.verify = row.verify;
                    this.video = resp.data.share_data.url;
                    this.getVideoBase64(resp.data.share_data.url).then(url => {
                        this.videoCover = url;
                    });
                    this.material_no = row.material_no;
                    this.detailDialog = true;
                } else {
                    this.$notify({
                        title: '提示',
                        message: resp.msg,
                        duration: 2000,
                        type: 'warning'
                    });
                }
            });
        },
        /**
         * 获取视频封面
         * @param url
         * @returns {Promise<unknown>}
         */
        getVideoBase64(url) {
            return new Promise(function (resolve, reject) {

                let dataURL = '';
                let video = document.createElement("video");

                video.setAttribute('crossOrigin', 'anonymous');//处理跨域
                video.setAttribute('src', url);
                video.setAttribute('width', 400);
                video.setAttribute('height', 240);
                video.setAttribute('autoplay', 'autoplay');
                video.pause();
                video.addEventListener('loadeddata', function () {
                    let canvas = document.createElement("canvas"),
                        width = video.width, //canvas的尺寸和图片一样
                        height = video.height;

                    canvas.width = width;
                    canvas.height = height;
                    canvas.getContext("2d").drawImage(video, 0, 0, width, height); //绘制canvas
                    dataURL = canvas.toDataURL('image/jpeg'); //转换为base64
                    resolve(dataURL);
                });
            });
        },
        /**
         * 查看视频
         */
        previewVideo() {
            this.left = this.$refs.wrapper.clientWidth - 300;
            this.$refs.videoSimple.showVideo();
        },
    }
};
</script>

<style scoped lang="less">
.wrapper {
    width: 100%;

    .content-wrapper {
        display: flex;
        height: calc(100% - 24px);
        flex-direction: column;
        position: relative;
        margin: 12px;
        background: #fff;
        box-sizing: border-box;

        /*header*/

        .header {
            height: 68px;
            padding: 24px;
            box-sizing: border-box;
            box-shadow: 0px -1px 0px 0px #F0F2F5;
            display: flex;
            align-items: center;
            justify-content: space-between;

            .header-title {
                color: #333333;
                font-size: 20px;
                height: 20px;
                font-weight: 500;
                position: relative;
                margin: 24px 0 24px 12px;

                &::before {
                    content: '';
                    position: absolute;
                    left: -8px;
                    top: -1px;
                    width: 3px;
                    height: 20px;
                    background: #1890FF;
                }
            }
        }

        /*el-tabs*/

        /deep/ .el-tabs {

            .el-tabs__nav-wrap {
                height: 54px;
                padding-left: 24px;

                .el-tabs__nav-scroll {
                    .el-tabs__nav {
                        height: 54px;
                        line-height: 54px;
                    }
                }
            }

            .el-tabs__header {
                margin-bottom: 0;
            }
        }

        /*筛选*/

        .filter-view {
            box-sizing: border-box;
            display: flex;
            align-items: center;
            flex-wrap: wrap;
            padding: 24px 24px 0;

            .header-new-box {
                display: flex;
                align-items: center;
                color: #606266;
                white-space: nowrap;
                margin-right: 32px;
                margin-bottom: 24px;

                &-title {
                    margin-right: 2px;
                    white-space: nowrap;
                }
            }
        }

        /*表格商品信息*/

        .goods-info {
            display: flex;
            align-items: center;

            .goods-img {
                width: 62px;
                height: 62px;
                border-radius: 4px;
                cursor: pointer;
            }

            .goods-name {
                margin-left: 10px;
                overflow: hidden;
                text-overflow: ellipsis;
                display: -webkit-box;
                -webkit-box-orient: vertical;
                -webkit-line-clamp: 2;
            }

            .goods-price {
                margin-left: 10px;
                font-size: 14px;
                font-weight: 400;
                color: #333333;
            }
        }

        /* 分页 */

        .pagination-wrapper {
            padding: 10px 32px;

            .el-pagination {
                float: right;
                padding: 0;
                background-color: transparent;
            }
        }

        /deep/ .el-dialog__header {
            padding-top: 10px;
            box-shadow: 0px 1px 0px 0px rgba(233, 233, 233, 1);
            display: flex;
            align-items: center;
            background: #FAFAFA;
            border-radius: 4px 4px 0 0;

            .el-dialog__title {
                font-weight: 500;
                color: #333;
                font-size: 14px;
            }

            .el-icon-close:before {
                color: rgb(194, 194, 194);
                font-size: 17px;
                font-weight: bold;
            }

            .el-dialog__headerbtn {
                top: 14px;
            }
        }

        /deep/ .el-textarea__inner {
            resize: none;
        }
    }

    /*弹窗*/

    .form-title {
        margin: 16px 0;
        font-size: 14px;
        color: #333;
        position: relative;
    }

    /deep/ .el-dialog__header {
        padding-top: 10px;
        box-shadow: 0px 1px 0px 0px rgba(233, 233, 233, 1);
        display: flex;
        align-items: center;
        background: #FAFAFA;
        border-radius: 4px 4px 0 0;

        .el-dialog__title {
            font-weight: 500;
            color: #333;
            font-size: 14px;
        }

        .el-icon-close:before {
            color: rgb(194, 194, 194);
            font-size: 17px;
            font-weight: bold;
        }

        .el-dialog__headerbtn {
            top: 14px;
        }
    }

    /deep/ .el-textarea__inner {
        resize: none;
    }

    /*驳回原因弹窗*/

    .reject-reason {
        display: flex;
        flex-direction: column;
        align-items: center;

        .reason-wrapper {
            width: 100%;
            padding: 24px;
            background: #FBFBFB;
            border-radius: 4px;
            border: 1px solid #EEEEEE;
            line-height: 20px;
            margin: 4px 34px;
        }

        .know-btn {
            width: 120px;
            margin-top: 30px;
        }
    }

    /*查看详情弹窗*/

    .detail-dialog {
        margin: 0 32px;

        .goods-table {
            border-radius: 4px;
            border: 1px solid #DCDFE6;

            .goods-info {
                display: flex;
                align-items: center;

                .goods-img {
                    width: 62px;
                    height: 62px;
                    border-radius: 4px;
                    cursor: pointer;
                }

                .goods-name {
                    margin-left: 10px;
                    overflow: hidden;
                    text-overflow: ellipsis;
                    display: -webkit-box;
                    -webkit-box-orient: vertical;
                    -webkit-line-clamp: 2;
                }

                .goods-price {
                    margin-left: 10px;
                    font-size: 14px;
                    font-weight: 400;
                    color: #333333;
                }
            }
        }

        .content {
            display: flex;
            flex-direction: column;
            padding-bottom: 40px;

            .content-item {
                margin-top: 24px;
                display: flex;
                font-size: 14px;
                font-weight: 400;
                line-height: 20px;

                .item-title {
                    color: #888888;
                    flex-shrink: 0;
                }

                .item-view {
                    color: #333333;
                    flex-grow: 1;
                }

                /*图片列表*/

                .pic-list {
                    display: flex;
                    flex-wrap: wrap;

                    .pic-item {
                        width: 100px;
                        height: 100px;
                        border-radius: 4px;
                        overflow: hidden;
                        margin-right: 16px;
                        margin-bottom: 16px;
                        cursor: pointer;

                        img {
                            width: 100%;
                            height: 100%;
                        }
                    }
                }

                /*视频*/

                .video-wrap {
                    width: 105px;
                    height: 105px;
                    display: flex;
                    flex-wrap: wrap;
                    position: relative;
                    cursor: pointer;

                    .video {
                        height: 100%;
                        background: #FFFFFF;
                        border-radius: 4px;
                        overflow: hidden;

                        img {
                            width: 100%;
                            height: 100%;
                        }
                    }

                    .icon {
                        width: 40px;
                        height: 40px;
                        position: absolute;
                        z-index: 1;
                        left: 50%;
                        top: 50%;
                        transform: translate(-50%, -50%);
                    }
                }
            }
        }

        .btn-group {
            justify-content: center;
            display: flex;

            .el-button {
                width: 126px;
                margin: 0 26px;
            }
        }
    }
    .reject-dialog {
        /deep/ .el-dialog__footer {
            text-align: center;
            padding: 0;
            padding-bottom: 30px;
            margin-top: 6px;
        }
        /deep/ .el-dialog__body {
            padding: 24px 64px;
        }
    }
}
</style>
